package com.zsqat.eshop.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zsqat.eshop.product.entity.AttrEntity;
import com.zsqat.eshop.product.service.AttrAttrgroupRelationService;
import com.zsqat.eshop.product.service.AttrService;
import com.zsqat.eshop.product.service.CategoryService;
import com.zsqat.eshop.product.vo.AttrGroupRelationVo;
import com.zsqat.eshop.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zsqat.eshop.product.entity.AttrGroupEntity;
import com.zsqat.eshop.product.service.AttrGroupService;
import com.zsqat.common.utils.PageUtils;
import com.zsqat.common.utils.R;

/**
 * 属性分组
 *
 * @author zsqat
 * @email 491192853@qq.com
 * @date 2020-11-19 16:40:59
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
  @Autowired
  private AttrGroupService attrGroupService;

  @Autowired
  private CategoryService categoryService;

  @Autowired
  private AttrService attrService;

  @Autowired
  private AttrAttrgroupRelationService attrRelationService;

  // product/attrgroup/{catelogId}/withattr
  // 获取给定分类下所有分组以及关联属性
  // 发布商品第二步根据查到的分组以及分组关联的属性动态展示界面供管理员添加规格参数
  @GetMapping(value = "/{categoryId}/withattr")
  public R getAttrGroupWithAttrs(@PathVariable("categoryId") Long categoryId) {
    // 1.查出当前分类下的所有属性分组
    // 2.查出每个属性分组下的所有属性
    List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrGroupWithAttrsByCategoryId(categoryId);
    return R.ok().put("data", attrGroupWithAttrsVos);
  }

  // 获取属性分组下有关联的属性
  // /product/attrgroup/' + this.attrGroupId + '/attr/relation
  @GetMapping("/{attrGroupId}/attr/relation")
  // @RequiresPermissions("product:attrgroup:list")
  public R attrWithRelationList(@RequestParam Map<String, Object> params,
                                @PathVariable("attrGroupId") Long attrGroupId) {
    List<AttrEntity> entities = attrService.getRelationAttr(attrGroupId);
    return R.ok().put("data", entities);
  }

  // 获取属性分组下没有关联的属性(新建关联时，就要获取分组下没有关联的属性)
  // /product/attrgroup/' + this.attrGroupId + '/noattr/relation
  @GetMapping("/{attrgroupId}/noattr/relation")
  public R attrNoRelation(@RequestParam Map<String, Object> params,
                          @PathVariable("attrgroupId") Long attrgroupId) {
    PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
    return R.ok().put("page", page);
  }

  // 批量添加属性与分组关联关系
  // /product/attrgroup/attr/relation
  @PostMapping(value = "/attr/relation")
  public R addRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos) {
    attrRelationService.saveBatch(attrGroupRelationVos);
    return R.ok();
  }

  // 移除分组和属性之间的关联
  ///product/attrgroup/attr/relation/delete
  @PostMapping(value = "/attr/relation/delete")
  public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
    attrRelationService.deleteRelation(vos);
    return R.ok();
  }

  /**
   * 列表
   */
  @RequestMapping("/list/{categoryId}")
  // @RequiresPermissions("product:attrgroup:list")
  public R listByCategoryId(@RequestParam Map<String, Object> params, @PathVariable long categoryId) {
    PageUtils page = attrGroupService.queryPage(params, categoryId);
    return R.ok().put("page", page);
  }

  /**
   * 信息
   */
  @RequestMapping("/info/{attrGroupId}")
  // @RequiresPermissions("product:attrgroup:info")
  public R info(@PathVariable("attrGroupId") Long attrGroupId) {
    AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
    // 需要把级联菜单完整路径反显到前台修改页面
    Long categoryId = attrGroup.getCategoryId();
    Long[] categoryPath = categoryService.findCategoryPath(categoryId);
    attrGroup.setCategoryPath(categoryPath);
    return R.ok().put("attrGroup", attrGroup);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  // @RequiresPermissions("product:attrgroup:save")
  public R save(@RequestBody AttrGroupEntity attrGroup) {
    attrGroupService.save(attrGroup);
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  // @RequiresPermissions("product:attrgroup:update")
  public R update(@RequestBody AttrGroupEntity attrGroup) {
    attrGroupService.updateById(attrGroup);
    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  // @RequiresPermissions("product:attrgroup:delete")
  public R delete(@RequestBody Long[] attrGroupIds) {
    attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
    return R.ok();
  }
}
